package Stana;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.text.IconView;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Market extends JavaDBFReaderTest implements ItemListener,ActionListener
{

	   JFrame chartview=null;
	   double price,quantity;
	   JMenuBar mb;
	   String[]items={"File","Algorithm","View","Help"};
	   JMenu item[]=new JMenu[4];
	   JMenuItem[]mitems=new JMenuItem[13];
	   String []mitem={"import Data","Open   Ctr+O","Save   Ctr+S","Print   Ctr+P","Exit     Ctr+X","K-means      Alt+K","Hierarchial   Alt+l","Meta Data   Alt+M","Data              Alt+T","Charts          Alt+C","Report          Alt+R","Stana Tutorial   Alt+N", "About Stana      AltB"};
	   JPanel []pa=new JPanel[6];
	   
	   JLabel lchart,lselect,lconfig,ltitle,yaxisname,xaxisname,algoname,clustername,ldistance,mincluster,maxcluster,summary,algorithem,distancemathod,numcluster,showsumary,showalgo,showmethod,shownumbcluster;
	   String[]chartdigram={"Select Chart","Pie Chart","Histogram","Area Chart","Scatter"};
	   String[]algo={"Select Algorithm","K-means","Hierarchical"};
	   String[]dis={"Select Distance","Euclidean Distance","City Block Distance"};
       JCheckBox cquantity,cprice,cdate;
	   JTextField title,xaxis,yaxis,miniumemcluseter,maximumcluster;
	   JComboBox chart,algorithm,distance;
	   JButton ok,cancel,submit;
	   Font font;
	   DefaultPieDataset pieDataset;
	   DefaultCategoryDataset areachart;
	   DefaultCategoryDataset dataset;
	   XYSeries seris;
	public Market()
	{
		chartview=new JFrame();
		chartview.setTitle("Chartview");
		chartview.setLayout(null);
		chartview.setSize(500,600);
		//chartview.setLayout(new GridLayout(1,2,3,3));
		chartview.setDefaultCloseOperation(2);
		 addMenuBar();
		chartview.setVisible(true);
	}
	   public void addMenuBar()
	   {
	      mb=new JMenuBar();
		  chartview.setJMenuBar(mb);
		  addItem();
	   }
	    int ctr=0;
		int key=1;
	   public void addItem()
	   {
	      for(int i=0;i<items.length;i++)
		  {
		     item[i]=new JMenu(items[i]);
			 mb.add(item[i]);
		  }
		  
		  for(int i=0;i<mitem.length;i++)
		  {
		      mitems[i]=new JMenuItem(mitem[i]);
			   if(i==5||i==7||i==11)
			       
				   ctr++;
				   item[ctr].add(mitem[i]);
			   
		  }
		  addPanel();
	   }
	   public void addPanel()
	   {
		   for(int c=0;c<pa.length;c++)
		   {
			   pa[c]=new JPanel();
			   pa[c].setLayout(null);
			   
			   pa[c].setBorder(BorderFactory .createTitledBorder(""));
			   chartview.getContentPane().add(pa[c]);
		   }
		   pa[0].setBounds(0,0,300,700);
		   pa[1].setBounds(305,0,800,700);
		   pa[2].setBounds(1110, 0,250,700);
		   pa[3].setBounds(5,5,780,250);
		   pa[4].setBounds(5,0,800,700);
		   pa[5].setBounds(5,5,780,250);
		   pa[5].setVisible(false);
		   pa[4].setVisible(false);
		   pa[1].add(pa[3]);
		   pa[1].add(pa[5]);
		  
		   addLabel();
	   } 
	   
	   public void addLabel()
	   {
		  lchart=new JLabel("Chart");
		  lchart.setBounds(5, 10,80,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  lchart.setFont(font);
		  pa[0].add(lchart);
		  
		  lselect=new JLabel("Select");
		  lselect.setBounds(5,40,80,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  lselect.setFont(font);
		  pa[0].add(lselect);
		  
		  lconfig=new JLabel("Chart Configuration");
		  lconfig.setBounds(5,100,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  lconfig.setFont(font);
		  pa[0].add(lconfig);
		  
		  ltitle=new JLabel("Title");
		  ltitle.setBounds(5,130,80,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  ltitle.setFont(font);
		  pa[0].add(ltitle);
		  
		  xaxisname=new JLabel("X-axisname");
		  xaxisname.setBounds(5,160,100,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  xaxisname.setFont(font);
		  pa[0].add(xaxisname);
		  
		  yaxisname=new JLabel("Y-axisname");
		  yaxisname.setBounds(5,190,100,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  yaxisname.setFont(font);
		  pa[0].add(yaxisname);
		  
		  
		  algoname=new JLabel("Algorithm Name:");
		  algoname.setBounds(10,5,150,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  algoname.setFont(font);
		  pa[3].add(algoname);

		  clustername=new JLabel("Cluster Name:");
		  clustername.setBounds(250,5,150,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  clustername.setFont(font);
		  pa[3].add(clustername);
		  
		  cquantity=new JCheckBox("Quantity");
		  cquantity.setBounds(10, 30, 100, 25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  cquantity.setFont(font);
		  pa[3].add(cquantity);
		 
		  cprice=new JCheckBox("Price");
		  cprice.setBounds(10, 60, 100, 25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  cprice.setFont(font);
		  pa[3].add(cprice);
		 
		  cdate=new JCheckBox("Date");
		  cdate.setBounds(10, 90, 100, 25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  cdate.setFont(font);
		  pa[3].add(cdate);
		  addTextField();
		  
		  ldistance=new JLabel("Distance Measures");
		  ldistance.setBounds(10,120,220,30);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  ldistance.setFont(font);
		  pa[3].add(ldistance);
		  
		  distance=new JComboBox(dis);
		  distance.setBounds(170,120,200,30);
		  distance.addItemListener(this);
		  pa[3].add(distance);
		  
		  mincluster=new JLabel("Minimum Of Cluster:");
		  mincluster.setBounds(10,155,220,30);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  mincluster.setFont(font);
		  pa[3].add(mincluster);
		  
		  miniumemcluseter=new JTextField();
		  miniumemcluseter.setBounds(170,155,200,30);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  miniumemcluseter.setFont(font);
		  pa[3].add(miniumemcluseter);
		  
		  maxcluster=new JLabel("Maximun Of Cluster:");
		  maxcluster.setBounds(10,190,220,30);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  maxcluster.setFont(font);
		  pa[3].add(maxcluster);
		  
		  maximumcluster=new JTextField();
		  maximumcluster.setBounds(170,190,200,30);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  maximumcluster.setFont(font);
		  pa[3].add(maximumcluster); 
		  
		  summary=new JLabel("Summary:");
		  summary.setBounds(10,5,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  summary.setFont(font);
		  pa[5].add(summary);
		  
		  showsumary=new JLabel("Showsummary");
		  showsumary.setBounds(170,5,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  showsumary.setFont(font);
		  pa[5].add(showsumary);
		  
		  algorithem=new JLabel("Algorithm:");
		  algorithem.setBounds(10,30,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  algorithem.setFont(font);
		  pa[5].add(algorithem);
		  
		  showalgo=new JLabel("Showlgorithm");
		  showalgo.setBounds(170,30,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  showalgo.setFont(font);
		  pa[5].add(showalgo);
		  
		  distancemathod=new JLabel("Distance Method:");
		  distancemathod.setBounds(10,55,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  distancemathod.setFont(font);
		  pa[5].add(distancemathod);
		  
		  showmethod=new JLabel("Showlmethod");
		  showmethod.setBounds(170,55,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  showmethod.setFont(font);
		  pa[5].add(showmethod);
		  
		  numcluster=new JLabel("Number Of Cluster:");
		  numcluster.setBounds(10,85,200,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  numcluster.setFont(font);
		  pa[5].add(numcluster);
		  
		  shownumbcluster=new JLabel("Showcluster");
		  shownumbcluster.setBounds(170,85,250,25);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  shownumbcluster.setFont(font);
		  pa[5].add(shownumbcluster);
		  
		  submit=new JButton("OK");
		  submit.setBounds(150,120,80,30);
		  submit.addActionListener(this);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  submit.setFont(font);
		  pa[5].add(submit);
		  
		  ok=new JButton("OK");
		  ok.setBounds(265,40,100,30);
		  ok.addActionListener(this);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  ok.setFont(font);
		  pa[3].add(ok); 
		  
		  cancel=new JButton("Cancel");
		  cancel.setBounds(265,75,100,30);
		  cancel.addActionListener(this);
		  font=new Font("Times New Roman",Font.ITALIC,18);
		  cancel.setFont(font);
		  pa[3].add(cancel); 
		  
	   }
	   public void addTextField()
	   {
		   chart=new JComboBox(chartdigram);
		   chart.setBounds(90,5,200,30);
		   chart.addItemListener(this);
		   pa[0].add(chart);
		   
		   algorithm=new JComboBox(algo);
		   algorithm.setBounds(90,40,200,30);
		   algorithm.addItemListener(this);
		   pa[0].add(algorithm);
		   
		   title=new JTextField("Enter Title");
		   title.setBounds(90,130,200,30);
		   font=new Font("Times New Roman",Font.ITALIC,18);
		   title.setFont(font);
		   pa[0].add(title);
		   
		   xaxis=new JTextField("Enter X-axisname");
		   xaxis.setBounds(90,160,200,30);
		   font=new Font("Times New Roman",Font.ITALIC,18);
		   xaxis.setFont(font);
		   pa[0].add(xaxis);
		   
		   yaxis=new JTextField("Enter Y-axisname");
		   yaxis.setBounds(90,190,200,30);
		   font=new Font("Times New Roman",Font.ITALIC,18);
		   yaxis.setFont(font);
		   pa[0].add(yaxis);
		   
	   }
	  
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Market();
		
		
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		Object selectitem=(JComboBox)e.getSource();
		/*if(selectitem==chart)
		{
			String showchart=chart.getSelectedItem().toString();
			if(showchart=="Pie Chart")
			{
				pa[1].removeAll();
				pieChart();
				pa[1].setBackground(Color.WHITE);
				pa[1].invalidate();
			}
		   if(showchart=="Area Chart")
			{
			   pa[1].removeAll();
				areaChart();
				pa[1].setBackground(new Color(129,111,212));
				pa[1].invalidate();
			}
		   if(showchart=="Histogram")
		   {
			   pa[1].removeAll();
			   histogramChart();
			   pa[1].setBackground(Color.LIGHT_GRAY);
			   pa[1].invalidate();
		   }
		   if(showchart=="Scatter")
		   {
			   pa[1].removeAll();
			   scatterChart();
			   pa[1].setBackground(new Color(129,101,112));
			   pa[1].invalidate();
		   }  
		}*/
		if(selectitem==algorithm)
		{
			String selectalgo=algorithm.getSelectedItem().toString();
			if(selectalgo=="K-means")
			{
				clustername.setText(algorithm.getSelectedItem().toString());
			}
			if(selectalgo=="Hierarchical")
			{
				clustername.setText(algorithm.getSelectedItem().toString());
			}
		}
	}
	public void pieChart()
	{
		
		  pieDataset= new DefaultPieDataset();
		  pieDataset.setValue("Price", price);
		  pieDataset.setValue("Quantity", quantity);
		  JFreeChart chart=ChartFactory.createPieChart3D("Pie Chart",pieDataset,true, true,true);
		  chart.setTitle(title.getText().toString());
		  PiePlot3D p=(PiePlot3D)chart.getPlot();
		  ChartPanel cp=new ChartPanel(chart);
		  cp.setBounds(15,220,700,350);
		  cp.setVisible(true);
		  pa[4].add(cp);
		
	}
    public void areaChart()
    {
    	areachart=new DefaultCategoryDataset();
    	areachart.setValue(price, "Total","Price");
    	areachart.setValue(quantity, "Total","Quantity");
    	JFreeChart chart=ChartFactory.createAreaChart("Total All Values","Super Market", "X Values", areachart,PlotOrientation.VERTICAL, true, true, false);
    	ChartPanel areapanel=new ChartPanel(chart);
    	areapanel.setBounds(15,5,750,650);
    	areapanel.setVisible(true);
    	pa[4].add(areapanel);
    	
    }
   /* public void histogramChart()
    {	
    	dataset = new DefaultCategoryDataset();
    	dataset.setValue(price, "Price Profit", "Nov");
    	dataset.setValue(quantity, "Quantity Profit", "Nov");
    	// Profit1, Profit2 represent the row keys
    	// Jane, Tom, Jill, etc. represent the column keys
    	JFreeChart chart = ChartFactory.createBarChart3D( "Comparison between Salesman",
    	"Salesman", "Value ($)", dataset, PlotOrientation.VERTICAL, true, true, false );
    	ChartPanel gram=new ChartPanel(chart);
    	gram.setBounds(15, 5, 750, 650);
    	gram.setVisible(true);
    	pa[4].add(gram);	
    }*/
    public void scatterChart(){
    	
    	XYSeriesCollection scatter=new XYSeriesCollection();
    	seris=new XYSeries("Scatter");
    	seris.add(price,quantity);
    	scatter.addSeries(seris);
    	JFreeChart chart=ChartFactory.createScatterPlot("Scatter Chart", "demo","demo1", scatter, PlotOrientation.VERTICAL, true,true,false);
    	ChartPanel scart=new ChartPanel(chart);
    	scart.setBounds(15, 5, 750,650);
    	scart.setVisible(true);
    	pa[4].add(scart);
    }
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stubif
		
		JButton show=(JButton)e.getSource();
		
		if(show==ok)
		{
		    	pa[3].setVisible(false);
		    	
		    	showsumary.setText(chart.getSelectedItem().toString());
		    	showalgo.setText(algorithm.getSelectedItem().toString());
		    	showmethod.setText(distance.getSelectedItem().toString());
		    	shownumbcluster.setText("Max:"+maximumcluster.getText().toString()+"\t\t"+"Min:"+miniumemcluseter.getText().toUpperCase());
		    	
			    pa[5].setVisible(true);
		}
		if(show==submit)
		{
			pa[5].setVisible(false);
			pa[4].setVisible(true);
		}
		
	}
    
}
